﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EELibrary.Collision2D;

public class TestCollisionTool : MonoBehaviour
{

    public bool result = false;
    public bool result2 = false;

    public Transform transA, transB, transC;
    public float radiusA = 5f, radiusB = 1f;
    public float theta = 10;
    public float width, height;
    
    private Vector2[] rectangle_points = null;
    private Vector2[] other_rectangle_points = null;

    public int generatePointNum = 21;


    public int lineNum = 4;

    

    void drawSector()
    {
        if (transA == null) return;
        // 生成点
        Vector2[] points = new Vector2[generatePointNum];
        points[0] = transA.position;
        int realy_point_num = generatePointNum - 1;
        float each_angle = theta * 2 / realy_point_num;

        Vector2 dir = transA.up;
        Vector3 zAxis = -transA.forward;


        float _angle = each_angle * realy_point_num / 2;
        int halfCount = realy_point_num / 2;
        int index = 1;
        for (; index <= halfCount; index++)
        {
            Quaternion qua = Quaternion.AngleAxis(_angle, zAxis);
            Vector2 pos = qua * dir * radiusA;
            pos = pos + points[0];
            points[index] = pos;
            _angle -= each_angle;
        }
        _angle = -each_angle;
        for (; index <= realy_point_num; index++)
        {
            Quaternion qua = Quaternion.AngleAxis(_angle, zAxis);
            Vector2 pos = qua * dir * radiusA;
            pos = pos + points[0];
            points[index] = pos;
            _angle -= each_angle;
        }
        drawPoints(points);
    }

    void drawSectorLine()
    {
        if (transA == null) return;
        float totalangle = theta * 2f;
        float each_angle = totalangle / (lineNum - 1);
        Vector2 dir = transA.up;
        float angle = -theta;
        Vector3 zAxis = -transA.forward;
        Vector2[] array_pos = new Vector2[lineNum];
        Vector2 srcpos = transA.position;

        for (int i = 0; i < lineNum; i++)
        {
            Quaternion qua = Quaternion.AngleAxis(angle, zAxis);
            Vector2 pos = qua * dir * radiusA;
            pos = pos + srcpos;
            array_pos[i] = pos;
            angle += each_angle;
        }
        Gizmos.color = Color.yellow;
        for (int i = 0; i < lineNum; i++)
        {
            Gizmos.DrawLine(srcpos, array_pos[i]);
        }
    }


    void drawCircle()
    {
        if (transB == null) return;

        if (result || result2)
        {
            Gizmos.color = Color.red;
        }
        else
        {
            Gizmos.color = Color.white;
        }

        Gizmos.DrawWireSphere(transB.position, radiusB);
    }

    void drawRectangle()
    {
        if (transC == null) return;
        if (rectangle_points == null) rectangle_points = new Vector2[4];
        Vector2 forward = transC.up;
        Vector2 pos = transC.position;
        Quaternion qua = Quaternion.AngleAxis(90, -transC.forward);
        Vector2 right = qua * forward;
        rectangle_points[0] = pos + right * width / 2;
        rectangle_points[1] = pos - right * width / 2;
        rectangle_points[2] = rectangle_points[1] + forward * height;
        rectangle_points[3] = rectangle_points[0] + forward * height;

        if(other_rectangle_points == null)
            other_rectangle_points = new Vector2[3];
        other_rectangle_points[0] = pos + forward * height / 2f;
        other_rectangle_points[1] = pos + forward * height;
        other_rectangle_points[2] = rectangle_points[0] + forward * height / 2f;
        
        drawPoints(rectangle_points);

        Vector2 rectangle_center = other_rectangle_points[0];
        Vector2 rectangle_width_center = other_rectangle_points[1];
        Vector2 rectangle_height_center = other_rectangle_points[2];
        Gizmos.color = Color.white;
        float size = 0.1f;
        Gizmos.DrawWireCube(rectangle_center, new Vector3(size, size, size));
        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(rectangle_center, rectangle_width_center);
        Gizmos.color = Color.red;
        Gizmos.DrawLine(rectangle_center, rectangle_height_center);

    }

    void drawPoints(Vector2[] points)
    {
        if (points == null || points.Length <= 2) return;
        int len = points.Length;
        for (int i = 0; i < len; i += 1)
        {
            if (i + 1 < len)
            {
                Gizmos.DrawLine(points[i], points[i + 1]);
            }
        }
        Gizmos.DrawLine(points[0], points[len - 1]);
    }



    private void OnDrawGizmos()
    {
        drawSector();
        drawRectangle();
        drawCircle();
        drawSectorLine();
        checkSectorAndCircle();
        checkRectangleAndCircle();
    }

    private void checkSectorAndCircle()
    {
        if (transA == null || transB == null) return;
        Vector2 sector_center = transA.position;
        Vector2 sector_dir = transA.up;
        Vector2 circle_center = transB.position;
        result = Collision2DTool.IsSectorDiskIntersect(sector_center, sector_dir, theta * Mathf.Deg2Rad, radiusA, circle_center, radiusB);
    }

    private void checkRectangleAndCircle()
    {
        if (transC == null || transB == null) return;
        result2 = Collision2DTool.IsCircleIntersectRectangle(other_rectangle_points[0], other_rectangle_points[1], other_rectangle_points[2], transB.position, radiusB);

    }
}
